package com.ssy.lingxi.report.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.api.enums.CommonBooleanEnum;
import com.ssy.lingxi.report.entity.report.MemberMobileMenuDO;
import com.ssy.lingxi.report.model.bo.MemberMobileAppMenuBO;
import com.ssy.lingxi.report.model.bo.MemberMobileMenuBO;
import com.ssy.lingxi.report.model.constant.MobileAppMenuEnum;
import com.ssy.lingxi.report.model.constant.MobileMenuEnum;
import com.ssy.lingxi.report.model.vo.request.MemberMobileMenuUpdateVO;
import com.ssy.lingxi.report.model.vo.response.MemberMobileAppMenuVO;
import com.ssy.lingxi.report.model.vo.response.MemberMobileMenuVO;
import com.ssy.lingxi.report.repository.report.MemberMobileMenuRepository;
import com.ssy.lingxi.report.service.IMemberMobileMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员手机端菜单接口实现
 * @author 伍将
 * @date 2021/3/3
 * @version 2.0.0
 */
@Service
public class MemberMobileMenuServiceImpl implements IMemberMobileMenuService {

    @Resource
    private MemberMobileMenuRepository repository;

    /**
     * 获取会员菜单
     * @author 伍将
     * @date 2021/3/3
     * @param sysUser:当前登录用户
     * @return 操作结果
     **/
    @Override
    public Wrapper<List<MemberMobileMenuVO>> getMemberMenuList(UserLoginCacheDTO sysUser) {

        List<MemberMobileMenuVO> resultList;

        // 查询会员菜单
        MemberMobileMenuDO menu = repository.findByMemberIdAndRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
        // 会员未配置时，使用系统默认,菜单按照sort从低到高排序，应用菜单默认打开
        if (null == menu || null == menu.getMenuList() || menu.getMenuList().size() <= 0) {
            resultList = Arrays.stream(MobileMenuEnum.values())
                    .sorted(Comparator.comparingInt(MobileMenuEnum::getSort))
                    .map(o -> {
                        // 组装菜单
                        MemberMobileMenuVO menuVO = new MemberMobileMenuVO();
                        menuVO.setCode(o.getCode());
                        menuVO.setName(o.getName());
                        menuVO.setSort(o.getSort());
                        // 组装内部App菜单
                        List<MemberMobileAppMenuVO> appList = Arrays.stream(MobileAppMenuEnum.values())
                                .filter(app -> app.getMenuCode().equals(o.getCode()))
                                .sorted(Comparator.comparingInt(MobileAppMenuEnum::getSort))
                                .map(item -> {
                                    MemberMobileAppMenuVO appMenuVO = new MemberMobileAppMenuVO();
                                    appMenuVO.setCode(item.getCode());
                                    appMenuVO.setName(item.getName());
                                    appMenuVO.setSort(item.getSort());
                                    appMenuVO.setLinkUrl(item.getLinkUrl());
                                    appMenuVO.setIsOpen(CommonBooleanEnum.YES.getCode());
                                    return appMenuVO;
                                }).collect(Collectors.toList());
                        menuVO.setAppList(appList);
                        return menuVO;
                    }).collect(Collectors.toList());
        } else {
            resultList = menu.getMenuList().stream()
                    .sorted(Comparator.comparingInt(MemberMobileMenuBO::getSort))
                    .map(o -> {
                        // 组装菜单
                        MemberMobileMenuVO menuVO = new MemberMobileMenuVO();
                        menuVO.setCode(o.getCode());
                        MobileMenuEnum menuEnum = MobileMenuEnum.getItem(o.getCode());
                        if (null != menuEnum) {
                            menuVO.setName(menuEnum.getName());
                        }
                        menuVO.setSort(o.getSort());
                        // 组装内部App菜单
                        List<MemberMobileAppMenuVO> appMenuVOList = o.getAppList().stream()
                                .sorted(Comparator.comparingInt(MemberMobileAppMenuBO::getSort))
                                .map(app -> {
                                    MemberMobileAppMenuVO appMenuVO = new MemberMobileAppMenuVO();
                                    appMenuVO.setCode(app.getCode());
                                    appMenuVO.setSort(app.getSort());
                                    MobileAppMenuEnum appMenuEnum = MobileAppMenuEnum.getItem(app.getCode());
                                    if (null != appMenuEnum) {
                                        appMenuVO.setName(appMenuEnum.getName());
                                        appMenuVO.setLinkUrl(appMenuEnum.getLinkUrl());
                                    }
                                    appMenuVO.setIsOpen(app.getIsOpen());
                                    return appMenuVO;
                                }).collect(Collectors.toList());
                        menuVO.setAppList(appMenuVOList);
                        return menuVO;
                    }).collect(Collectors.toList());
        }

        return Wrapper.success(resultList);
    }

    /**
     * 更新会员菜单
     * @author 伍将
     * @date 2021/3/3
     * @param sysUser:
     * @param updateList: 当前登录用户
     * @return 操作结果
     **/
    @Override
    public Wrapper<Void> updateMemberMenu(UserLoginCacheDTO sysUser, List<MemberMobileMenuUpdateVO> updateList) {
        // 查询会员菜单
        MemberMobileMenuDO menu = repository.findByMemberIdAndRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
        if (null == menu) {
            menu = new MemberMobileMenuDO();
            menu.setMemberId(sysUser.getMemberId());
            menu.setRoleId(sysUser.getMemberRoleId());
        }
        List<MemberMobileMenuBO> menuList = updateList.stream().map(o -> {
            MemberMobileMenuBO menuBO = new MemberMobileMenuBO();
            menuBO.setCode(o.getCode());
            menuBO.setSort(o.getSort());
            List<MemberMobileAppMenuBO> appMenuList = o.getAppList().stream().map(app -> {
                MemberMobileAppMenuBO appMenuBO = new MemberMobileAppMenuBO();
                appMenuBO.setCode(app.getCode());
                appMenuBO.setSort(app.getSort());
                appMenuBO.setIsOpen(app.getIsOpen());
                return appMenuBO;
            }).collect(Collectors.toList());
            menuBO.setAppList(appMenuList);
            return menuBO;
        }).collect(Collectors.toList());
        menu.setMenuList(menuList);

        repository.saveAndFlush(menu);

        return Wrapper.success();
    }
}
